//
//  PopoverPresentationController.swift
//  新浪微博
//
//  Created by 李赛 on 15/5/15.
//  Copyright (c) 2015年 李赛. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    /// 展现视图的大小
    var presentedFrame : CGRect = CGRectZero
    
    //  遮罩视图
    lazy var dummyView : UIView = {
       let v = UIView()
        // 设置背景
        v.backgroundColor = UIColor(white: 0, alpha: 0.2)
        // 添加手势识别,可以隔离所有底层控件
        let tap = UITapGestureRecognizer(target: self, action: "dummyClick")
        v.addGestureRecognizer(tap)
        
        return v
        
    }()
    
    // 关闭modal视图
    func dummyClick (){
        // 关闭modal视图
        self.presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override init(presentedViewController: UIViewController!, presentingViewController: UIViewController!) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
//        println(presentedViewController)
//        println(presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 设置遮罩视图的大小
        dummyView.frame = containerView.bounds
        containerView.insertSubview(dummyView, atIndex: 0)
        
        // 设置目标视图的大小
        presentedView().frame = presentedFrame
    }
}
